/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.wizards;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.ui.ide.IDE;

import com.cea.papyrus.core.IPapyrusUIConstants;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.core.utils.DiResourceSet;



/**
 * Create new new .uml2-file.
 * Those files can be used with the PapyrusEditor (see plugin.xml).
 */
public class CreateProfileWizard extends Wizard implements INewWizard {
	
	/**
	 * 
	 */
	public static final String WIZARD_ID = IPapyrusUIConstants.WIZARD_ID+".createprofile"; 
	
	/**
	 * 
	 */
	private static int fileCount = 1;
	
	/**
	 * 
	 */
	private CreationPage page1;
	
	/**
	 * 
	 */
	private int debugChannel=1;
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.IWizard#addPages()
	 */
	/**
	 * 
	 */
	@Override
	public void addPages() {
		// add pages to this wizard
		Debug.debug(this," Calls PapyrusCreationWizard.addPage", debugChannel);
		
		addPage(page1); 
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
	 */
	/**
	 * 
	 * 
	 * @param workbench 
	 * @param selection 
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// create pages for this wizard
		Debug.debug(this," Calls PapyrusCreationWizard.init", debugChannel);
		
		page1 = new CreationPage(workbench, selection); 
		
		
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.IWizard#performFinish()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public boolean performFinish() {
		Debug.debug(this," Calls PapyrusCreationWizard.performFinish", debugChannel);
		return page1.finish();
		
	}
	
	/**
	 * This WizardPage can create an empty .uml2 file for the PapyrusEditor.
	 */
	private class CreationPage extends WizardNewFileCreationPage {
		
		/**
		 * 
		 */
		private static final String PROFILE_EXTENSION = IPapyrusUIConstants.PROFILE_EXTENSION;
		
		/**
		 * 
		 */
		private final IWorkbench workbench;
		
		/**
		 * Create a new wizard page instance.
		 * 
		 * @param workbench the current workbench
		 * @param selection the current object selection
		 * 
		 * @see PapyrusdCreationWizard#init(IWorkbench, IStructuredSelection)
		 */
		CreationPage(IWorkbench workbench, IStructuredSelection selection) {
			
			super("umlCreationPage1", selection);
			this.workbench = workbench;
			setTitle("Create a new empty Profile");
			setDescription("Create a new Profile");
			
			Debug.debug(this," Calls CreationPage()", debugChannel);
			
		}
		
		/* (non-Javadoc)
		 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createControl(org.eclipse.swt.widgets.Composite)
		 */
		/**
		 * 
		 * 
		 * @param parent 
		 */
		@Override
		public void createControl(Composite parent) {
			super.createControl(parent);
			Debug.debug(this,"Calls CreationPage.createControl", debugChannel);
			setFileName("umlExample" + fileCount + PROFILE_EXTENSION);
			setPageComplete(validatePage());
			
			
		}
		
		
		
		
		/**
		 * This method will be invoked, when the "Finish" button is pressed.
		 * 
		 * @return 
		 * 
		 * @see PapyrusCreationWizard#performFinish()
		 */
		boolean finish() {
			Debug.debug(this," Calls CreationPage.finish", debugChannel);
			
			// create a new file, result != null if successful
			IFile newFile = createNewFile();
			fileCount++;
			
			// open newly created file in the editor
			IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
			if ((newFile != null) && (page != null)) {
				// Create needed object
				DiResourceSet resources = new DiResourceSet(null);
				resources.createProfileResources(newFile);
				
				//ModelResourceManager modelResource= new ModelResourceManager(resources);
				//modelResource.createModelsResource2(newFile);
				try {
					resources.saveResources();
				} catch (IOException e1) {
					e1.printStackTrace();
					return false;
				}

				
				try {
					IDE.openEditor(page, newFile, true);
				} catch (PartInitException e) {
					e.printStackTrace();
					return false;
				}
			}
			return true;
		}
		
		/* 
		 * Create a model, and serialize it in a stream. This Stream is then provided
		 * to an editor.
		 */
		/**
		 * 
		 * 
		 * @return 
		 */
		@Override
		protected InputStream getInitialContents() {
			Debug.debug(this," Calls CreationPage.getInitialContents", debugChannel);
			
			ByteArrayInputStream bais = null;
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(baos);
				//oos.writeObject(createDefaultContent()); // argument must be Serializable
				oos.flush();
				oos.close();
				bais = new ByteArrayInputStream(baos.toByteArray());
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
			return bais;
		}
		
		/**
		 * Return true, if the file name entered in this page is valid.
		 * 
		 * @return 
		 */
		private boolean validateFilename() {
			Debug.debug(this," Calls CreationPage.validateFilename", debugChannel);
			
			if ((getFileName() != null) && getFileName().endsWith(PROFILE_EXTENSION)) {
				return true;
			}
			setErrorMessage("The 'file' name must end with " + PROFILE_EXTENSION);
			return false;
		}
		
		/* (non-Javadoc)
		 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validatePage()
		 */
		/**
		 * 
		 * 
		 * @return 
		 */
		@Override
		protected boolean validatePage() {
			Debug.debug(this," Calls CreationPage.validatePage", debugChannel);
			
			return super.validatePage() && validateFilename();
		}
	}
}
